package cn.sunshine.ymcc.web.controller;

import cn.sunshine.ymcc.domain.Login;
import cn.sunshine.ymcc.dto.LoginDto;
import cn.sunshine.ymcc.service.ILoginService;
import cn.sunshine.ymcc.query.LoginQuery; // 自我修改的模板位置
import cn.sunshine.ymcc.result.JSONResult; // 自我修改的模板位置
import cn.sunshine.ymcc.result.PageList; // 自我修改的模板位置
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    public ILoginService loginService;
   /*
   * @Title: loginCommon
   * @Description: Security登录成功之后返回的信息
   * @Author: Mr.Jiang
   * @Version: 1.0
   * @Date:  2022/8/27 23:42
   * @Parameters: [dto]
   * @Return cn.sunshine.ymcc.result.JSONResult
   */
    @PostMapping("/common")
    public JSONResult loginCommon(@RequestBody @Valid LoginDto loginDto){
        return loginService.loginCommon(loginDto);
    }

    /*
    * @Title: loginSuccess
    * @Description: 登录成功后重定向地址（Security认证：入门案例）
    * @Author: Mr.Jiang
    * @Version: 1.0
    * @Date:  2022/8/26 20:14
    * @Parameters: []
    * @Return java.lang.String
    */
    @RequestMapping(value="/loginSuccess",method= RequestMethod.POST)
    @ResponseBody
    public String loginSuccess(){
        return "登录成功";
    }

    /**
    * 保存和修改公用的
    */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public JSONResult saveOrUpdate(@RequestBody Login login){
        if(login.getId()!=null){
            loginService.updateById(login);
        }else{
            loginService.insert(login);
        }
        return JSONResult.success(login);
    }

    /**
    * 删除对象
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public JSONResult delete(@PathVariable("id") Long id){
        loginService.deleteById(id);
        return JSONResult.success();
    }

    /**
   * 获取对象
   */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public JSONResult get(@PathVariable("id")Long id){
        return JSONResult.success(loginService.selectById(id));
    }


    /**
    * 查询所有对象
    */

    @RequestMapping(value = "/list",method = RequestMethod.GET)
    @PreAuthorize("hasAuthority('employee:list')")
    public JSONResult list(){
        return JSONResult.success(loginService.selectList(null));
    }


    /**
    * 带条件分页查询数据
    */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public JSONResult page(@RequestBody LoginQuery query){
        Page<Login> page = new Page<Login>(query.getPage(),query.getRows());
        page = loginService.selectPage(page);
        return JSONResult.success(new PageList<Login>(page.getTotal(),page.getRecords()));
    }
}
